package com.example.ticketingserver.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ticketingserver.dto.R;
import com.example.ticketingserver.entity.*;
import com.example.ticketingserver.service.FavoriteService;
import com.example.ticketingserver.service.FavoritedetailsService;
import com.example.ticketingserver.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.baomidou.mybatisplus.extension.toolkit.Db.getOne;
import static com.baomidou.mybatisplus.extension.toolkit.Db.save;

@Tag(name = "收藏夹管理",description = "收藏夹创建，列表查看，修改，删除等操作")
@CrossOrigin
@RestController
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private UserService userService;
//    @Autowired
//    private FavoritedetailsService favoritedetailsService;


    @Operation(summary = "收藏夹添加",description = "需要提供收藏夹名称，标签")
    @PostMapping("/favorite/add")
    public R add(@Parameter(description = "收藏夹对象")@RequestBody Favorite favorite) {
        //获取查询条件构造器
        QueryWrapper<Favorite> query = Wrappers.query(Favorite.class);
        query.eq("name",favorite.getName());
        //执行查询
        Favorite one = getOne(query);
        if(Objects.nonNull(one)){
            return R.fail().setMsg("该收藏夹已存在！");
        }
        if(save(favorite)){
            return R.ok().setMsg("添加成功！").setData(favorite);
        }
        return R.fail().setMsg("添加失败！");
    }

    @Operation(summary = "分页查询收藏夹",description = "查询所有的收藏夹，为车站加入收藏夹提供数据接口")
    @Parameters({
            @Parameter(name = "page",description = "当前页码",required = true),
            @Parameter(name = "limit",description = "每页的条数",required = true),
    })
    @GetMapping("/favorite/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer limit){
        List<Favorite> list = favoriteService.list(new Page<Favorite>(page,limit));
        list.forEach(f->{
            Integer uid = f.getUid();
            User user = userService.getById(uid);
            f.setUser(user);


        });
        long count = favoriteService.count();
        return R.ok().setData(list).setCount(count);
    }
    @Operation(summary = "查询所有收藏夹",description = "所有收藏夹")
    @GetMapping("/favorite/listAll")
    public R queryAll(){
        List<Favorite> list = favoriteService.list();
        return R.ok().setMsg("查询成功").setData(list);
    }

    @Operation(summary = "收藏夹删除", description = "根据收藏夹id删除记录，该操作为物理删除，请谨慎处理")
    @DeleteMapping("/favorite/del/{id}")
    public R del(@Parameter(description = "收藏夹id") @PathVariable("id") Integer id){
        Favorite favorite = favoriteService.getById(id);
        if (Objects.isNull(favorite)){
            return R.fail().setMsg("该车站收藏夹不存在！");
        }
        return favoriteService.removeById(id) ? R.ok().setMsg("删除成功").setData(favorite) : R.fail().setMsg("删除失败");
    }
    @Operation(summary = "收藏夹修改", description = "根据id")
    @PutMapping("/favorite/update")
    public R modify(@Parameter(description = "收藏夹对象") Favorite favorite){

//        根据id修改管理员信息(务必传入管理员id)
        boolean b = favoriteService.updateById(favorite);
        return b ? R.ok().setMsg("修改成功！").setData(favorite) : R.fail().setMsg("修改失败！");
    }

}
